<%@include file="_header.jspf" %>
<%@page import="com.spartancoder.accommodation.models.University" %>
<%@page import="com.spartancoder.accommodation.models.College" %>
<%@page import="com.spartancoder.accommodation.models.Student" %>
<%@page import="com.spartancoder.accommodation.beans.UniversityBean" %>
<%@page import="com.spartancoder.accommodation.beans.CollegeBean" %>
<%@page import="com.spartancoder.accommodation.beans.StudentBean" %>
<%

int uId = 0;
String id = request.getParameter("uId");
if (id != null) {
    uId = Integer.parseInt(id);
}

String cId = request.getParameter("cId");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String cnp = request.getParameter("cnp");
String email = request.getParameter("email");
String password = request.getParameter("password");
String passwordConfirm = request.getParameter("passwordConfirm");
String error = null;

if(cId != null && firstName != null && 
   lastName !=null && cnp != null &&
   email != null && password != null) {

    if( firstName.trim().isEmpty() || lastName.trim().isEmpty() ||
        cnp.trim().isEmpty() || email.trim().isEmpty() ) {
        error = "Toate campurile sunt obligatorii";
    } else if( cnp.length() != 13 ) {
        error = "Cod numeric personal invalid";
    } else if( email.contains("@") == false ) {
        error = "Adresa de email invalida";
    } else if( password.equals(passwordConfirm) == false ) {
        error = "Confirmarea parolei nu corespunde";
    } else {
        StudentBean sb = new StudentBean();
        sb.setCollegeId(Integer.parseInt(cId));
        sb.setFirstName(firstName);
        sb.setLastName(lastName);
        sb.setCnp(cnp);
        sb.setEmail(email);
        sb.setActive(true);
        Student.create(sb, password);
        
        session.setAttribute("flash", "Studentul a fost adaugat");
        response.sendRedirect("list-student.jsp");
    }
} else {
    firstName = "";
    lastName = "";
    cnp = "";
    email = "";
    cId = "0";
}

%>

<h2>Adaugare student</h2>

<form action="add-student.jsp" class="span-10" method="post">
    <fieldset>
    <select class="text large" name="uId">
        <% for(UniversityBean ub : University.fetchAll()) { %>
            <option <% if(ub.getUniversityId() == uId) {%>selected="selected"<%}%>
            value="<%=ub.getUniversityId()%>"><%=ub.getName()%></option>
        <% } %>
    </select>
    <button type="submit" class="button positive">Ok</button>
    </fieldset>
</form>

<% if (uId != 0) { %>
<form action="add-student.jsp" method="post" class="span-10">
<fieldset>
    <input type="hidden" name="uId" value="<%=uId%>" />

    <% if(error != null) { %>
        <br /><div class="error"><%=error%></div>
    <% } %>
    <label class="large text">Facultate:</label><br />
    <select name="cId" class="large text">
    <% for(CollegeBean cb : College.fetchAll(uId)) { %>
    <option <% if(Integer.parseInt(cId) == cb.getCollegeId()) {%>selected="selected"<%}%>
        value="<%=cb.getCollegeId()%>"><%=cb.getName()%></option>
    <% } %>
    </select><br />

    <label class="large text">Prenume:</label><br />
    <input type="text" name="firstName" class="large text" value="<%=firstName%>" /><br />

    <label class="large text">Nume:</label><br />
    <input type="text" name="lastName" class="large text" value="<%=lastName%>" /><br />

    <label class="large text">Cnp:</label><br />
    <input type="text" name="cnp" class="large text" /><br />

    <label class="large text">Email:</label><br />
    <input type="text" name="email" class="large text" /><br />

    <label class="large text">Parola:</label><br />
    <input type="password" name="password" class="large text" /><br />

    <label class="large text">Confirmare parola:</label><br />
    <input type="password" name="passwordConfirm" class="large text" /><br />

    <button type="submit" class="button positive">
        Adaugare student
    </button>

</fieldset>
</form>
<% } %>

<%@include file="_footer.jspf" %>